package com.ccit.paas.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccit.paas.domain.StudentPO;
import com.ccit.paas.dto.StudentDTO;
import com.ccit.paas.dto.StudentRequestDTO;
import com.ccit.paas.mapper.StudentMapper;
import com.ccit.paas.service.IStudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 学生信息服务接口实现，  ServiceImpl类实现了IService接口封装增删改查的相关接口
 *
 * @author: chinacoalit.com
 * @date 2024/1/9 15:53
 * @version: v1.0 初始化
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentPO> implements IStudentService {


    /**
     * 添加
     * @param stu
     * @return java.lang.Integer
     * @exception
     * @author chinacoalit.com
     * @date 2024/1/9 15:54
     */
    @Override
    @DS("slave")
    public Integer add(StudentDTO stu) {
        stu.setId(IdWorker.getIdStr());
        StudentPO studentPO = new StudentPO();
        BeanUtils.copyProperties(stu,studentPO);
        boolean save = this.save(studentPO);
        return save?1:0;
    }

    /**
     * 分页查询
     * @param studentRequestDTO 参数请求类
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.ccit.paas.dto.StudentDTO>
     * @exception
     * @author chinacoalit.com
     * @date 2024/5/28 10:38
     */
    @Override
    public Page<StudentDTO> getStudentPageList(StudentRequestDTO studentRequestDTO){
        QueryWrapper<StudentPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",studentRequestDTO.getName());
        queryWrapper.eq("sex",studentRequestDTO.getSex());
        queryWrapper.orderByDesc("name");

        Page page = new Page<>(1, 1);
        Page<StudentPO> pageList = this.page(page,queryWrapper);

        //封装返回结果集  建议封装一个工具类
        Page<StudentDTO> retPage = new Page<>();
        if(null != pageList && pageList.getSize() > 0){
            List<StudentDTO> studentDTOList = new ArrayList<>();
            for(StudentPO stuPO : pageList.getRecords()){
                StudentDTO stuDTO = new StudentDTO();
                BeanUtils.copyProperties(stuPO,stuDTO);
                studentDTOList.add(stuDTO);
            }
            retPage.setRecords(studentDTOList);
            retPage.setSize(page.getSize());
            retPage.setCurrent(page.getCurrent());
            retPage.setTotal(page.getTotal());
        }
        return retPage;
    }


    /**
     * 获取集合数据
     * @param
     * @return java.util.List<com.ccit.paas.dto.StudentDTO>
     * @exception
     * @author chinacoalit.com
     * @date 2024/5/28 15:11
     */
    @Override
    public List<StudentDTO> getList(){
        List<StudentPO> list = this.list();
        if(null != list && list.size() > 0){
            List<StudentDTO> retList = new ArrayList<>();
            for(StudentPO stuPO : list){
                StudentDTO stuDTO = new StudentDTO();
                BeanUtils.copyProperties(stuPO,stuDTO);
                retList.add(stuDTO);
            }
            return retList;
        }
        return null;
    }
}
